package com.htjc.integral.conversion.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.commons.service.util.StringUtil;
import com.htjc.integral.conversion.mapper.EcBpMemberDetailMapper;
import com.htjc.integral.conversion.mapper.EcBpMemberdetailExpandMapper;
import com.htjc.integral.conversion.service.IEcIntegralDetailService;
import com.htjc.integral.model.conversion.EcBpMemberDetail;
import com.htjc.integral.model.conversion.EcBpMemberNew;
import com.htjc.integral.model.conversion.EcBpMemberdetailExpand;
import com.htjc.integral.model.conversion.EcBpUseTime;

import gwjf.Gwjf;
import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "conversionLogger")
@Service
public class EcIntegralDetailServiceImpl implements IEcIntegralDetailService {
	
	@Autowired
	private EcBpMemberDetailMapper ecBpMemberDetailMapper;
	
	@Autowired
	private EcBpMemberdetailExpandMapper ecBpMemberdetailExpandMapper;
	
//	private static final Logger log = LoggerFactory.getLogger(EcIntegralDetailServiceImpl.class);

//	private static final String IS_ALLOW_QUKUAILIAN = SgccMallUtil.getInstance().getConfValue("is.allow.qukuailian.qkl");
//	//区块链积分比例
//	private static final String QKL_INTEGRAL_SCALE = SgccMallUtil.getInstance().getConfValue("qkl.integral.scale");
//	//区块链ip
//	private static final String QKL_INTEGRAL_IP = SgccMallUtil.getInstance().getConfValue("qkl.integral.ip");
	
	private static final String IS_ALLOW_QUKUAILIAN = "";
	//区块链积分比例
	private static final String QKL_INTEGRAL_SCALE = "";
	//区块链ip
	private static final String QKL_INTEGRAL_IP = "";

	//111
	@Override
	public String changeIntegralAndGlobalLock(boolean isPresent ,
			Map<String, Object> userMap, Map<String, String> sourceMap) throws Exception {
		String resultStr = "";//返回信息
		log.info("调用积分变更通用方法主要传参为："+"isPresent:"+isPresent
				+"  totalNum:"+userMap.get("totalNum")+"  userId:"+userMap.get("userId")+"  originId:"+userMap.get("originId"));
//		boolean flag = false;
		Integer totalNum = (Integer)userMap.get("totalNum");
		String userId = (String)userMap.get("userId");
		Calendar calendar = Calendar.getInstance();
		String extvar = (String) userMap.get("extvar");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
//		String lockKey = "EXCHANGE_INTEGRAL_GMK_".concat(userId);
		try {
			//设置全局锁
//			flag = spyMemcachedClient.getMemcachedClient().add(lockKey, 60, "lock").get();
//			if(flag==false){
//				return "请稍后重试!";
//			}
			
			//赠送积分
			if(isPresent){
				//增加区块链兑入方法
				if(StringUtils.isNotBlank(IS_ALLOW_QUKUAILIAN) && IS_ALLOW_QUKUAILIAN.equals("true")){
					if(sourceMap.get("channelIdCreate").equals("JD_GANGBENG")){
						try {
							Gwjf gwjf = new Gwjf();
							//设置环境ip
							String[] ipTel = QKL_INTEGRAL_IP.split(":");
	                		String ip = ipTel[0];
	                		String port = ipTel[1];
	                		gwjf.configServerUrl(ip, port);

	                		String jdinfo = (String) sourceMap.get("channelIdCreate"); // 公司信息
							String companyId = (String) sourceMap.get("channelIdCreate"); // 公司编号
							int amount_op = totalNum / 200; // 对方积分数量
							int amount = totalNum; // 积分数量
							String message = "";
							String recordId1 = (String) userMap.get("originId"); // 积分兑入订单号
							if(!QKL_INTEGRAL_SCALE.equals("200")){
								// 设置积分比例兑换
								System.out.println("-----------设置比例-----------------------");
								message = gwjf.requestSetCompanyExchangeRate(companyId, 1, Integer.valueOf(QKL_INTEGRAL_SCALE), jdinfo);
								log.info("区块链接入设置比例：" + message);
							}
							// 积分兑入
							System.out.println("--------------积分兑入----------------------");
							message = gwjf.requestBuyGwpoints(companyId, amount_op, userId, recordId1);
							log.info("区块链接入积分兑入" + message);
						} catch (Exception e) {
							// TODO: handle exception
							log.error("区块链接入异常:"+e.getMessage());
						}
					}
				}
				
				calendar.setTime(new Date());
				int year = calendar.get(Calendar.YEAR);
				Map<String, String>useTimeMap = new HashMap<String, String>();
				useTimeMap.put("bizcodeIdCreate", (String)sourceMap.get("bizCodeIdCreate"));
				useTimeMap.put("channelIdCreate", (String)sourceMap.get("channelIdCreate"));
				List<EcBpUseTime> useTimeList = ecBpMemberDetailMapper.getUseTimeById(useTimeMap);
				int makeUse = 0;
				int keepUse = 365*3;
				if(useTimeList.size()>0){
					EcBpUseTime ecBpUseTime = useTimeList.get(0);
					log.info("获取有效使用："+ecBpUseTime.toString());
					 makeUse = (ecBpUseTime.getMakeUse() !=null)?ecBpUseTime.getMakeUse():0;
					 keepUse = (ecBpUseTime.getKeepUse() !=null)?ecBpUseTime.getKeepUse():(365*3);
				}
				EcBpMemberDetail ecBpMemberDetail = new EcBpMemberDetail();
				String detailId = (UUID.randomUUID().toString()).replace("-", "");
				ecBpMemberDetail.setDetailId(detailId);
				ecBpMemberDetail.setMemberId(userId);
				ecBpMemberDetail.setCreateTime(new Date());
				ecBpMemberDetail.setBpValue(totalNum);
				ecBpMemberDetail.setOriginId((String)userMap.get("originId"));
				ecBpMemberDetail.setOriginRemark((String)userMap.get("remark"));
				if(userMap.get("orgNo") != null && StringUtil.isNotNull(userMap.get("orgNo")))
				{
					ecBpMemberDetail.setMerchantId((String)userMap.get("orgNo"));
				}
				ecBpMemberDetail.setBpYear(year);//增加积分存储当前年份
				ecBpMemberDetail.setScaleTrans((Integer)userMap.get("scaleTrans"));
				ecBpMemberDetail.setBpTransition((String)userMap.get("bpTransition"));
				if(userMap.get("boundCorId") != null && StringUtil.isNotNull(userMap.get("boundCorId")))
				{
					ecBpMemberDetail.setBoundId((String)userMap.get("boundCorId"));
				}
				ecBpMemberDetail.setAccIsNo((String)userMap.get("accIsNo"));
				ecBpMemberDetail.setBpValueRemain(totalNum);
				ecBpMemberDetail.setBizcodeIdCreate((String)sourceMap.get("bizCodeIdCreate"));
				ecBpMemberDetail.setChannelIdCreate((String)sourceMap.get("channelIdCreate"));
				if(StringUtil.isNotNull(userMap.get("orgNo"))) {
					if (userMap.get("orgNo").equals("120100")) {
						
						log.info("是否进入天津日期修改----------------------");
						//获取到 extvar 截取交易日期
						ArrayList extList=new ArrayList();
				    	String str =extvar;
				    	String[] strs=str.split("\\|");
				    	for(int i=0,len=strs.length;i<len;i++){
				    	 
				    		extList.add(strs[i]);
				    	}
				    	 if (extList.size() > 3 ) {
				    	 String Tjjytime = (String) extList.get(3);
				    	//将字符串转成Date类型
				    	 String date1= null;
				    	 Date date2=null;
						 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
						 SimpleDateFormat sdf2=new SimpleDateFormat("yyyyHHddHHmmss");
						 date1 = sdf1.format(sdf2.parse(Tjjytime));
						 date2 =sdf1.parse(date1);
                         Date nextMonthFirstDate = nextTjMonthFirstDate(date2);
                         log.info("符合天津修改的生效日期----------------------"+nextMonthFirstDate);
                         ecBpMemberDetail.setStartTime(nextMonthFirstDate);
				    	 }else{
				    		 log.info("不符合天津修改的生效日期照旧执行----------------------"+extList.size());
				    		 calendar.add(calendar.DATE, makeUse);
							 ecBpMemberDetail.setStartTime(calendar.getTime());
				    		 
				    	 }
					}else{
						log.info("不是天津的则正常执行----------------------");
						calendar.add(calendar.DATE, makeUse);
						ecBpMemberDetail.setStartTime(calendar.getTime());
					}
				}else{
					calendar.add(calendar.DATE, makeUse);
					ecBpMemberDetail.setStartTime(calendar.getTime());
					
				}
				calendar.add(Calendar.DATE, keepUse);
				ecBpMemberDetail.setEndTime(calendar.getTime());
				ecBpMemberDetail.setBpIsFrozen("0");//积分未冻结
				ecBpMemberDetail.setBpChangeType("0");//积分增加
				if(userMap.get("consNo")!=null)
				{ecBpMemberDetail.setPowerUserNo((String) userMap.get("consNo"));}
				else {ecBpMemberDetail.setPowerUserNo("");};
				if(userMap.get("orgNo")!=null)
				{ecBpMemberDetail.setProvinceId((String) userMap.get("orgNo"));}else
				{ecBpMemberDetail.setProvinceId("");}
				ecBpMemberDetailMapper.insertMemberDetail(ecBpMemberDetail);
				//插入积分流水明细详情表
				insertEcBpMemberdetailExpand(ecBpMemberDetail, userMap);
				
				//变更用户总积分表
				List<EcBpMemberNew> ecBpMemberNewList = ecBpMemberDetailMapper.getBpValueById(userId);
				if(ecBpMemberNewList == null || ecBpMemberNewList.size() <=0 ){
					//插入
					Map<String, Object>memberNewMap = new HashMap<String, Object>();
					memberNewMap.put("memberId", userId);
					memberNewMap.put("bpValue", totalNum);
					ecBpMemberDetailMapper.insertMemberNew(memberNewMap);
				}
				else{
					//更新
					Map<String, Object>memberNewMap = new HashMap<String, Object>();
					memberNewMap.put("memberId", userId);
					memberNewMap.put("bpValue", totalNum);
					memberNewMap.put("version", ecBpMemberNewList.get(0).getVersion());
					Integer upback = ecBpMemberDetailMapper.updateMemberNewById(memberNewMap);
					if(upback <= 0){
						throw new RuntimeException("请稍后再试！");
					}
				}				
				resultStr = "增加积分成功";
			}
			//扣减积分
			if(!isPresent && totalNum<0){
				log.info("进入扣减积分开始++++++++++++++++++++++++++++++++++++++++++++===");
				Integer totalValue = totalNum * (-1);//需要扣减的积分数
				Integer userTotalValue = 0;
				Map<String, String>map = new HashMap<String, String>();
				map.put("bpIsFrozen", "0");//非冻结积分
				map.put("bpChangeType", "0");//增加类型积分信息
				map.put("currentTime", sdf.format(new Date()));//当前时间
				map.put("bpValueRemain", "0");
				map.put("memberId", userId);
				List<EcBpMemberDetail> memberDetailList = ecBpMemberDetailMapper.getMemberDetail(map);
				for (EcBpMemberDetail ecBpMemberDetail : memberDetailList) {
					userTotalValue += ecBpMemberDetail.getBpValueRemain();
				}
				log.info("现在积分用户："+userId+" 积分为："+userTotalValue);
				if(memberDetailList.size()<=0 || totalValue > userTotalValue){
					return "对不起,您的积分余额不足!";
				}
				
				//增加区块链兑出方法
				log.info("是否允许接入区块链:"+IS_ALLOW_QUKUAILIAN);
				System.out.println("bizCodeIdConsume:"+sourceMap.get("bizCodeIdConsume"));
				if(StringUtils.isNotBlank(IS_ALLOW_QUKUAILIAN) && IS_ALLOW_QUKUAILIAN.equals("true")){
					if(sourceMap.get("channelIdConsume").equals("JD_GANGBENG")){
						try {
							Gwjf gwjf = new Gwjf();
							//设置环境ip
							String[] ipTel = QKL_INTEGRAL_IP.split(":");
	                		String ip = ipTel[0];
	                		String port = ipTel[1];
	                		gwjf.configServerUrl(ip, port);
	                		
							String jdinfo = (String) sourceMap.get("channelIdConsume"); // 公司信息
							String companyId = (String) sourceMap.get("channelIdConsume"); // 公司编号
							int amount_op = totalNum * (-1) / 200; // 对方积分数量
							int amount = totalNum * (-1); // 积分数量
							String message = "";
							String recordId2 = (String) userMap.get("originId"); // 积分兑出订单号
							if(!QKL_INTEGRAL_SCALE.equals("200")){
								// 设置积分比例兑换
								System.out.println("-----------设置比例-----------------------");
								message = gwjf.requestSetCompanyExchangeRate(companyId, 1, Integer.valueOf(QKL_INTEGRAL_SCALE), jdinfo);
								log.info("区块链接入设置比例：" + message);
							}
							// 积分同步
							int GwAmount = userTotalValue;
							System.out.println("------------积分同步------------------------");
							message = gwjf.requestSyncPoints(GwAmount, userId);
							log.info("区块链接入积分同步：" + message);
							// 积分兑出
							int outamount = amount_op;//兑出积分数量
							System.out.println("-------------积分兑出------------------------");
							message = gwjf.requestSellGwPoints(companyId, outamount, userId, recordId2);
							log.info("区块链接入积分兑出：" + message);
						} catch (Exception e) {
							// TODO: handle exception
							log.error("区块链接入异常："+e.getMessage());
						}
					}
				}
				
				for (EcBpMemberDetail ecBpMemberDetail : memberDetailList) {
					if(ecBpMemberDetail.getBpValueRemain() >= totalValue && totalValue >0){
						//Integer remainValue = ecBpMemberDetail.getBpValueRemain() - totalValue;
						EcBpMemberDetail ecBpMemberDetail2 = new EcBpMemberDetail();
						//更新增加积分信息的剩余积分
						Map<String, Object>updateMap = new HashMap<String, Object>();
						updateMap.put("detailId", ecBpMemberDetail.getDetailId());
						updateMap.put("totalValue", totalValue *(-1));
						updateMap.put("version", ecBpMemberDetail.getVersion());
						Integer upback = ecBpMemberDetailMapper.updateMemberDetailById(updateMap);
						if(upback <= 0){
							throw new RuntimeException("请稍后重试！");
						}
						//插入扣减积分信息
						String detailId = (UUID.randomUUID().toString()).replaceAll("-", "");
						ecBpMemberDetail2.setDetailId(detailId);
						ecBpMemberDetail2.setMemberId(userId);
						ecBpMemberDetail2.setBpValue(totalValue*(-1));
						ecBpMemberDetail2.setCreateTime(new Date());
						ecBpMemberDetail2.setOriginId((String)userMap.get("originId"));
						ecBpMemberDetail2.setOriginRemark((String)userMap.get("remark"));
						ecBpMemberDetail2.setMerchantId((String)userMap.get("merchantId"));
						ecBpMemberDetail2.setBpYear(ecBpMemberDetail.getBpYear());//记录关联生成的年份
						ecBpMemberDetail2.setBizcodeIdCreate(ecBpMemberDetail.getBizcodeIdCreate());
						ecBpMemberDetail2.setChannelIdCreate(ecBpMemberDetail.getChannelIdCreate());
						ecBpMemberDetail2.setBizcodeIdConsume((String)sourceMap.get("bizCodeIdConsume"));
						ecBpMemberDetail2.setChannelIdConsume((String)sourceMap.get("channelIdConsume"));
						ecBpMemberDetail2.setUseDetailId(ecBpMemberDetail.getDetailId());//存储使用的是那条信息的积分
						ecBpMemberDetail2.setBpChangeType("1");//积分扣减
						if(userMap.get("consNo")!=null)
						{ecBpMemberDetail.setPowerUserNo((String) userMap.get("consNo"));}
						else {ecBpMemberDetail.setPowerUserNo("");};
						if(userMap.get("orgNo")!=null)
						{ecBpMemberDetail.setProvinceId((String) userMap.get("orgNo"));}else
						{ecBpMemberDetail.setProvinceId("");}
						ecBpMemberDetailMapper.insertMemberDetail(ecBpMemberDetail2);//将积分扣减信息存进积分变更信息表
						
						//插入积分流水明细详情表
						insertEcBpMemberdetailExpand(ecBpMemberDetail2, userMap);
						
						break;
					}
					else {
						
						EcBpMemberDetail ecBpMemberDetail2 = new EcBpMemberDetail();
						//更新增加积分信息的剩余积分
						//Integer remainValue = 0;
						Map<String, Object>updateMap = new HashMap<String, Object>();
						updateMap.put("detailId", ecBpMemberDetail.getDetailId());
						updateMap.put("totalValue", totalValue *(-1));
						updateMap.put("version", ecBpMemberDetail.getVersion());
						Integer upback = ecBpMemberDetailMapper.updateMemberDetailById(updateMap);
						if(upback <= 0){
							throw new RuntimeException("请稍后重试！");
						}
						//插入扣减积分信息
						String detailId = (UUID.randomUUID().toString()).replaceAll("-", "");
						ecBpMemberDetail2.setDetailId(detailId);
						ecBpMemberDetail2.setMemberId(userId);
						ecBpMemberDetail2.setBpValue(ecBpMemberDetail.getBpValueRemain()*(-1));
						ecBpMemberDetail2.setCreateTime(new Date());
						ecBpMemberDetail2.setOriginId((String)userMap.get("originId"));
						ecBpMemberDetail2.setOriginRemark((String)userMap.get("remark"));
						ecBpMemberDetail2.setMerchantId((String)userMap.get("merchantId"));
						ecBpMemberDetail2.setBpYear(ecBpMemberDetail.getBpYear());//记录关联生成的年份
						ecBpMemberDetail2.setBizcodeIdCreate(ecBpMemberDetail.getBizcodeIdCreate());
						ecBpMemberDetail2.setChannelIdCreate(ecBpMemberDetail.getChannelIdCreate());
						ecBpMemberDetail2.setBizcodeIdConsume((String)sourceMap.get("bizCodeIdConsume"));
						ecBpMemberDetail2.setChannelIdConsume((String)sourceMap.get("channelIdConsume"));
						ecBpMemberDetail2.setUseDetailId(ecBpMemberDetail.getDetailId());//存储使用的是那条信息的积分
						ecBpMemberDetail2.setBpChangeType("1");//积分扣减
						if(userMap.get("consNo")!=null)
						{ecBpMemberDetail.setPowerUserNo((String) userMap.get("consNo"));}
						else {ecBpMemberDetail.setPowerUserNo("");};
						if(userMap.get("orgNo")!=null)
						{ecBpMemberDetail.setProvinceId((String) userMap.get("orgNo"));}else
						{ecBpMemberDetail.setProvinceId("");}
						ecBpMemberDetailMapper.insertMemberDetail(ecBpMemberDetail2);//将积分扣减信息存进积分变更信息表
						
						//插入积分流水明细详情表
						insertEcBpMemberdetailExpand(ecBpMemberDetail2, userMap);
						
						totalValue -= ecBpMemberDetail.getBpValueRemain();
					}
				}
				
				//变更用户总积分
				List<EcBpMemberNew> ecBpMemberNewList = ecBpMemberDetailMapper.getBpValueById(userId);
				log.info("=================变更用户总积分"+ecBpMemberNewList);
				Map<String, Object>memberNewMap = new HashMap<String, Object>();
				memberNewMap.put("memberId", userId);
				memberNewMap.put("bpValue", totalNum);
				memberNewMap.put("version", ecBpMemberNewList.get(0).getVersion());
				
				log.info("======变更用户总积分,参数为：memberId="+userId+";bpValue="+totalNum+";version="+ecBpMemberNewList.get(0).getVersion());
				Integer upback = ecBpMemberDetailMapper.updateMemberNewById(memberNewMap);
				if(upback <= 0){
					throw new RuntimeException("请稍后再试！");
				}
				resultStr= "扣减积分成功";
			}
			//返还积分
			if(!isPresent && totalNum>0){
				log.info("进入返还积分开始++++++++++++++++++++++++++++++++++++++++++++===");
				Map<String, String> map = new HashMap<String, String>();
				map.put("originId", (String)userMap.get("originId"));
				map.put("memberId", userId);
				List<EcBpMemberDetail> memberDetailList = ecBpMemberDetailMapper.getMemberDetail(map);
				log.info("进入返还积分++++++++++++++++++++++++++++++++++++++++++++memberDetailList"+memberDetailList);
				if(memberDetailList.size()<=0){
					log.info("进入返还积分++++++++++++++++++++++++++++++++++++++++++++===对不起,您没有可返还积分!");
					return "对不起,您没有可返还积分!";
				}
				for (EcBpMemberDetail ecBpMemberDetail : memberDetailList) {
					log.info("进入原路返还积分++++++++++++++++++++++++++++++++++++++++++++原路返还积分开始");
					//原路返还积分
					Integer bpValueChange = ecBpMemberDetail.getBpValue();
					Map<String, String>map2 = new HashMap<String, String>();
					map2.put("detailId", ecBpMemberDetail.getUseDetailId());
					//List<EcBpMemberDetail> memberDetailList2 = ecBpMemberDetailMapper.getMemberDetail(map2);
					//Integer remainValue = memberDetailList2.get(0).getBpValueRemain() +(bpValueChange * (-1));
					Map<String, Object>updateMap = new HashMap<String, Object>();
					updateMap.put("detailId", ecBpMemberDetail.getUseDetailId());
					updateMap.put("totalValue", bpValueChange * (-1));
					Integer upback = ecBpMemberDetailMapper.updateMemberDetailById(updateMap);
					log.info("进入返还积分++++++++++++++++++++++++++++++++++++++++++++upback"+upback);
					if(upback <= 0){
						throw new RuntimeException("请稍后重试！");
					}
					//插入变更信息
					EcBpMemberDetail ecBpMemberDetail2 = new EcBpMemberDetail();
					String detailId = (UUID.randomUUID().toString()).replaceAll("-", "");
					ecBpMemberDetail2.setDetailId(detailId);
					ecBpMemberDetail2.setMemberId(userId);
					ecBpMemberDetail2.setBpValue(bpValueChange *(-1));
					ecBpMemberDetail2.setCreateTime(new Date());
					ecBpMemberDetail2.setOriginId((String)userMap.get("originId"));
					ecBpMemberDetail2.setOriginRemark((String)userMap.get("remark"));
					ecBpMemberDetail2.setBpYear(ecBpMemberDetail.getBpYear());//记录关联生成的年份
					ecBpMemberDetail2.setBizcodeIdCreate(ecBpMemberDetail.getBizcodeIdCreate());
					ecBpMemberDetail2.setChannelIdCreate(ecBpMemberDetail.getChannelIdCreate());
					ecBpMemberDetail2.setBizcodeIdConsume((String)sourceMap.get("bizCodeIdConsume"));
					ecBpMemberDetail2.setChannelIdConsume((String)sourceMap.get("channelIdConsume"));
					ecBpMemberDetail2.setUseDetailId(ecBpMemberDetail.getUseDetailId());//存储使用的是那条信息的积分
					ecBpMemberDetail2.setBpChangeType("1");//积分返还
					if(userMap.get("consNo")!=null)
					{ecBpMemberDetail.setPowerUserNo((String) userMap.get("consNo"));}
					else {ecBpMemberDetail.setPowerUserNo("");};
					if(userMap.get("orgNo")!=null)
					{ecBpMemberDetail.setProvinceId((String) userMap.get("orgNo"));}else
					{ecBpMemberDetail.setProvinceId("");}
					ecBpMemberDetailMapper.insertMemberDetail(ecBpMemberDetail2);//将积分扣减信息存进积分变更信息表
					
					//插入积分流水明细详情表
					insertEcBpMemberdetailExpand(ecBpMemberDetail2, userMap);
					
					log.info("进入原路返还积分结束++++++++++++++++++++++++++++++++++++++++++++插入变更信息"+ecBpMemberDetail2);
				}
				//变更用户总积分
				List<EcBpMemberNew> ecBpMemberNewList = ecBpMemberDetailMapper.getBpValueById(userId);
				Map<String, Object>memberNewMap = new HashMap<String, Object>();
				memberNewMap.put("memberId", userId);
				memberNewMap.put("bpValue", totalNum);
				memberNewMap.put("version", ecBpMemberNewList.get(0).getVersion());
				log.info("进入返还积分++++++++++++++++++++++++++++++++++++++++++++变更用户总积分"+memberNewMap);
				Integer upback = ecBpMemberDetailMapper.updateMemberNewById(memberNewMap);
				if(upback <= 0){
					throw new RuntimeException("请稍后再试！");
				}
				resultStr = "返还积分成功";
				log.info("进入返还积分++++++++++++++++++++++++++++++++++++++++++++返还积分成功");
			}
		} finally {
//			spyMemcachedClient.getMemcachedClient().delete(lockKey);
		}
		return resultStr;
	}
	
	//111
	//插入积分流水明细详情表
	@Override
	public void insertEcBpMemberdetailExpand(EcBpMemberDetail ecBpMemberDetail, Map<String, Object> userMap)
	{
		EcBpMemberdetailExpand ecBpMemberdetailExpand = new EcBpMemberdetailExpand();
		ecBpMemberdetailExpand.setId(ecBpMemberDetail.getDetailId());
		if(userMap.get("consNo") != null && StringUtil.isNotNull(userMap.get("consNo")))
		{
			ecBpMemberdetailExpand.setPowerUserNo((String) userMap.get("consNo"));
		}
		if(userMap.get("orgNo") != null && StringUtil.isNotNull(userMap.get("orgNo")))
		{
			ecBpMemberdetailExpand.setProvinceId((String) userMap.get("orgNo"));
		}
		ecBpMemberdetailExpand.setOriginId(ecBpMemberDetail.getOriginId());
		ecBpMemberdetailExpand.setOriginRemark(ecBpMemberDetail.getOriginRemark());
		ecBpMemberdetailExpand.setBizcodeIdCreate(ecBpMemberDetail.getBizcodeIdCreate());
		ecBpMemberdetailExpand.setChannelIdCreate(ecBpMemberDetail.getChannelIdCreate());
		ecBpMemberdetailExpand.setBizcodeIdConsume(ecBpMemberDetail.getBizcodeIdConsume());
		ecBpMemberdetailExpand.setChannelIdConsume(ecBpMemberDetail.getChannelIdConsume());
		
		
		ecBpMemberdetailExpandMapper.insertSelective(ecBpMemberdetailExpand);
	}

	//111
	// 根据生效日期推后下月5号
	public static Date nextTjMonthFirstDate(Date date2) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date2);
		calendar.set(Calendar.DAY_OF_MONTH, 5);
		calendar.add(Calendar.MONTH, 1);
		return calendar.getTime();
	}
	
}
